package com.yanqu.road.server.cmd.activity.common;

import com.yanqu.road.entity.bag.UseCommonGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.bag.BagProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;


@Cmd(code = Protocol.S_COMMON_ACTIVITY_USE_GOODS, desc = "使用通用活动道具")
public class CommonActivityUseGoodsCmd extends AbstractCommand {

    private int goodsId;

    private long count;

    private long param;

    private UseCommonGoodsResult result = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BagProto.BagUseGoodsReqMsg reqMsg = BagProto.BagUseGoodsReqMsg.parseFrom(packet.getBytes());
        goodsId = reqMsg.getGoodsId();
        count = reqMsg.getCount();
        param = reqMsg.getParam();

        if (count <= 0) {
            return false;
        }

        // 强制最高10个道具
//        if (count > 10) {
//            count = 10;
//        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (count > 100) {
            ret = GameErrorCode.E_ERROR_PARAMETER;
            return;
        }

        if (count > 1 && count <= 10) {
            if (VipMgr.getVipLv(player.getModule(PlayerModule.class).getUserInfo().getVipExp()) < GameConfig.COMMON_ACTIVITY_TEN_VIP_LIMIT
                    && player.getTitleId() < GameConfig.COMMON_ACTIVITY_TEN_TITLE_LIMIT) {
                ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
                return;
            }
        }

        if (count > 10) {
            List<Integer> list = StringUtils.stringToIntegerList(GameConfig.COMMON_ACTIVITY_HUNDRED_VIP_LIMIT, ";");
            if (VipMgr.getVipLv(player.getModule(PlayerModule.class).getUserInfo().getVipExp()) < list.get(1)
                    && player.getTitleId() < list.get(0)) {
                ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
                return;
            }
        }

        BagModule bagModule = player.getModule(BagModule.class);
        UserBagItem bagItem = bagModule.getUserBagItem(goodsId);
        if (bagItem == null || bagItem.getNum() <= 0) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
        if (null == goodsInfo) {
            ret = GameErrorCode.E_GOODS_NO_EXIST;
            return;
        }
        if (goodsInfo.getCanUse() <= 0) {
            ret = GameErrorCode.E_BAG_ITEM_CAN_NO_USE;
            return;
        }
        result = player.getModule(ActivityMallModule.class).useCommonActivityGoods(goodsInfo, count);
    }

    @Override
    public void sendData(GamePlayer player) {
        BagProto.BagUseCommonGoodsRespMsg.Builder respMsg = BagProto.BagUseCommonGoodsRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.setGoodsId(goodsId);
        respMsg.setCount(count);
        if (0 == ret) {
            if (null != result) {
                respMsg.setRet(result.getRet());
                respMsg.setReward(result.getReward());
            } else {
                respMsg.setReward("");
            }
        }
        if (result == null) {
            respMsg.setSpecialReward(1);
        } else {
            respMsg.setSpecialReward(result.getSpecialReward());
        }

        player.sendPacket(Protocol.U_COMMON_ACTIVITY_USE_GOODS, respMsg);
    }
}
